package com.wmlive.hhvideo.heihei.channel;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.text.TextUtils;

import com.wmlive.hhvideo.heihei.beans.main.ShareInfo;
import com.wmlive.hhvideo.heihei.login.AccountUtil;

import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.PlatformActionListener;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.sina.weibo.SinaWeibo;
import cn.sharesdk.tencent.qq.QQ;
import cn.sharesdk.tencent.qzone.QZone;
import cn.sharesdk.wechat.friends.Wechat;
import cn.sharesdk.wechat.moments.WechatMoments;
import cn.wmlive.hhvideo.R;

/**
 * Created by XueFei on 2017/6/7.
 * <p>
 * 分享集成
 */

public class ShareSdkUtils {
    private static volatile ShareSdkUtils mShareSdkUtils;

    public static ShareSdkUtils getInstance() {
        if (mShareSdkUtils == null) {
            synchronized (ShareSdkUtils.class) {
                if (mShareSdkUtils == null) {
                    mShareSdkUtils = new ShareSdkUtils();
                }
            }
        }
        return mShareSdkUtils;
    }

    /**
     * 微博分享
     *
     * @param info
     * @param context
     * @param mListener
     */
    public void showWeiboShare(Context context, ShareInfo info, PlatformActionListener mListener) {
        try {
            SinaWeibo.ShareParams params = new SinaWeibo.ShareParams();
            params.setShareType(Platform.SHARE_IMAGE | Platform.SHARE_WEBPAGE);
            if (TextUtils.isEmpty(info.getShare_title())) {
                info.setShare_title(context.getString(R.string.share_title, "@动次"));
            }
            params.setTitle(info.getShare_title());
            if (TextUtils.isEmpty(info.getShare_weibo_desc())) {
                info.setShare_weibo_desc(context.getString(R.string.share_text));
            }
            params.setText(info.getShare_weibo_desc());
            if (TextUtils.isEmpty(info.getShare_url())) {
                info.setShare_url(context.getString(R.string.share_url));
            }
            params.setUrl(info.getShare_url());
            params.setTitleUrl(info.getShare_url());
            if (TextUtils.isEmpty(info.getShare_image_url())) {
                Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), R.mipmap.icon);
                params.setImageData(bitmap);
            } else {
                params.setImageUrl(info.getShare_image_url());
            }
            Platform sinaWeibo = ShareSDK.getPlatform(SinaWeibo.NAME);
            if (mListener != null) {
                sinaWeibo.setPlatformActionListener(mListener);
            }
            sinaWeibo.share(params);
        } catch (Exception e) {

        }
    }

    /**
     * QQ好友分享
     *
     * @param info
     * @param context
     * @param mListener
     */
    public void showQQShare(Context context, ShareInfo info, PlatformActionListener mListener) {
        try {
            QQ.ShareParams params = new QQ.ShareParams();
            params.setShareType(Platform.SHARE_WEBPAGE);
            if (TextUtils.isEmpty(info.getShare_title())) {
                info.setShare_title(context.getString(R.string.share_title, "@动次"));
            }
            params.setTitle(info.getShare_title());
            if (TextUtils.isEmpty(info.getShare_desc())) {
                info.setShare_desc(context.getString(R.string.share_text));
            }
            params.setText(info.getShare_desc());
            if (TextUtils.isEmpty(info.getShare_url())) {
                info.setShare_url(context.getString(R.string.share_url));
            }
            params.setUrl(info.getShare_url());
            params.setTitleUrl(info.getShare_url());
            if (TextUtils.isEmpty(info.getShare_image_url())) {
                Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), R.mipmap.icon);
                params.setImageData(bitmap);
            } else {
                params.setImageUrl(info.getShare_image_url());
            }
            Platform qq = ShareSDK.getPlatform(QQ.NAME);
            if (mListener != null) {
                qq.setPlatformActionListener(mListener);
            }
            qq.share(params);
        } catch (Exception e) {

        }
    }


    /**
     * QQ空间分享
     *
     * @param info
     * @param context
     * @param mListener
     */
    public void showQQZoneShare(Context context, ShareInfo info, PlatformActionListener mListener) {
        try {
            QZone.ShareParams params = new QZone.ShareParams();
            params.setShareType(Platform.SHARE_WEBPAGE);
            if (TextUtils.isEmpty(info.getShare_title())) {
                info.setShare_title(context.getString(R.string.share_title, "@动次"));
            }
            params.setTitle(info.getShare_title());
            if (TextUtils.isEmpty(info.getShare_desc())) {
                info.setShare_desc(context.getString(R.string.share_text));
            }
            params.setText(info.getShare_desc());
            if (TextUtils.isEmpty(info.getShare_url())) {
                info.setShare_url(context.getString(R.string.share_url));
            }
            params.setUrl(info.getShare_url());
            params.setTitleUrl(info.getShare_url());
            if (TextUtils.isEmpty(info.getShare_image_url())) {
                Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), R.mipmap.icon);
                params.setImageData(bitmap);
            } else {
                params.setImageUrl(info.getShare_image_url());
            }

            Platform zone = ShareSDK.getPlatform(QZone.NAME);
            if (mListener != null) {
                zone.setPlatformActionListener(mListener);
            }
            zone.share(params);
        } catch (Exception e) {

        }
    }

    /**
     * 微信朋友圈分享
     *
     * @param info
     * @param context
     * @param mListener
     */
    public void showWechatShare(Context context, ShareInfo info, PlatformActionListener mListener) {
        try {
            if (null == info) {
                info = new ShareInfo();
            }
            Wechat.ShareParams params = new Wechat.ShareParams();
            params.setShareType(Platform.SHARE_WEBPAGE);
            if (TextUtils.isEmpty(info.getShare_title())) {
                info.setShare_title(context.getString(R.string.share_title, "@动次"));
            }
            params.setTitle(info.getShare_title());
            if (TextUtils.isEmpty(info.getShare_desc())) {
                info.setShare_desc(context.getString(R.string.share_text));
            }
            params.setText(info.getShare_desc());
            if (TextUtils.isEmpty(info.getShare_url())) {
                info.setShare_url(context.getString(R.string.share_url));
            }
            params.setUrl(info.getShare_url());
            params.setTitleUrl(info.getShare_url());
            if (TextUtils.isEmpty(info.getShare_image_url())) {
                Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), R.mipmap.icon);
                params.setImageData(bitmap);
            } else {
                params.setImageUrl(info.getShare_image_url());
            }
            Platform wechat = ShareSDK.getPlatform(Wechat.NAME);
            if (mListener != null) {
                wechat.setPlatformActionListener(mListener);
            }
            wechat.share(params);
        } catch (Exception e) {

        }
    }

    /**
     * 微信好友分享
     *
     * @param info
     * @param context
     * @param mListener
     */
    public void showWechatMomentsShare(Context context, ShareInfo info, PlatformActionListener mListener) {
        try {
            WechatMoments.ShareParams params = new WechatMoments.ShareParams();
            params.setShareType(Platform.SHARE_WEBPAGE);
            if (TextUtils.isEmpty(info.getShare_title())) {
                info.setShare_title(context.getString(R.string.share_title, "@动次"));
            }
            params.setTitle(info.getShare_title());
            if (TextUtils.isEmpty(info.getShare_desc())) {
                info.setShare_desc(context.getString(R.string.share_text));
            }
            params.setText(info.getShare_desc());
            if (TextUtils.isEmpty(info.getShare_url())) {
                info.setShare_url(context.getString(R.string.share_url));
            }
            params.setUrl(info.getShare_url());
            params.setTitleUrl(info.getShare_url());
            if (TextUtils.isEmpty(info.getShare_image_url())) {
                Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), R.mipmap.icon);
                params.setImageData(bitmap);
            } else {
                params.setImageUrl(info.getShare_image_url());
            }
            Platform wechatMoments = ShareSDK.getPlatform(WechatMoments.NAME);
            if (mListener != null) {
                wechatMoments.setPlatformActionListener(mListener);
            }
            wechatMoments.share(params);
        } catch (Exception e) {

        }
    }
}
